package com.gin.no_gay_alliance.interceptor;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 拦截已经404到资源
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/9 10:42
 **/
public class ResourceNotFountInterceptor implements Interceptor {
    HashSet<String> notFoundUrl = new HashSet<>();

    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        String method = request.method();
        String url = request.url().toString();
        if ("get".equalsIgnoreCase(method) && notFoundUrl.contains(url)) {
            String message = "拦截404资源: " + url;
            Log.d(this.getClass().getSimpleName(), message);
            throw new IOException(message);
        }
        Response res = chain.proceed(request);
        if (res.code() == 404) {
            notFoundUrl.add(url);
        }
        return res;
    }
}
